// merchants/verifications/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    sumNumber: 1, //共有几张
    checkNumber: 1, //核销几张
    maxCount: 1,
    money: '', //应付金额
    couponInfo: {},
    cardInfo: {},
    showDialog: false,
    userid: 0,
    creatUid: 0,
    couponid: 0,
    coupontypeid: 0,
    couponsn: 0,
    cardid: 0,
    checkId: 0,
    payMoney: 0,
    remark: ''//核销备注
  },

  bindRemark: function (e) {
    this.setData({
      remark: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { //cardId:折扣卡id,couponInfo
    let that = this;
    let params = decodeURIComponent(options.scene);
    if (params && params != 'undefined') {//扫描小程序二维码进入
      let type = 8;
      let plist = '';
      if (params.indexOf('p-') != -1) {
        let param = params.match(/p-(\S*)/)[1];//参数
        if (params.indexOf('t-') != -1) {
          type = params.match(/t-(\S*)\*p/)[1];//路由
        }
        plist = param.split('_');
      } else {
        let param = params.match(/p=(\S*)/)[1];//参数
        if (params.indexOf('t=') != -1) {
          type = params.match(/t=(\S*)&p/)[1];//路由
        }
        plist = param.split('#');
      }
      if (type == 5) {//会员折扣卡
        wx.setStorageSync('cardId', plist[3]);
        that.getCouponOrCardInfo(plist[1], plist[2], '');
        that.data.cardid = plist[2];
        that.setData({
          creatUid: plist[0],
          userid: plist[1],
        })
      } else if (type == 8) {//优惠券
        that.getCouponOrCardInfo(plist[0], '', plist[1]);
        that.setData({
          userid: plist[0],
          coupontypeid: plist[1]
        })
        that.getCouponInfo(plist[2]);
        that.setData({
          couponid: plist[2]
        })
      }
    } else {
      if (options) {
        let cardid = '';
        let coupontypeid = '';
        if (options.cardId) {
          cardid = options.cardId
          that.data.cardid = cardid;
        }
        if (options.coupontypeid) {
          coupontypeid = options.coupontypeid;
          that.setData({
            coupontypeid: coupontypeid
          })
        }
        that.getCouponOrCardInfo(options.userid, cardid, coupontypeid);
        that.setData({
          creatUid: options.creatUid,
          userid: options.userid
        })
      }
      if (options.couponid) {
        that.getCouponInfo(options.couponid);
        that.setData({
          couponid: options.couponid
        })
      }
    }

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 获取核销的优惠券或卡信息
  getCouponOrCardInfo: function (userid, cardid, coupontypeid) {
    let that = this;
    let cardId = 0;
    let couponTypeId = 0;
    let maxCount = 1;
    if (cardid) {
      cardId = cardid;
    }
    if (coupontypeid) {
      couponTypeId = coupontypeid;
    }
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCountByMoney',
      data: {
        uid: userid,
        couponTypeId: couponTypeId,
        cardId: cardId//折扣卡id
      },
      method: 'POST',
      success: function (res) {
        if (res.data.Code == 0) {
          let cardInfo = res.data.Data.card;
          let couponInfo = res.data.Data.couponType;
          if (res.data.Data.card) {
            // 折扣卡
            cardInfo.Discount = (cardInfo.Discount * 0.1).toFixed(1);
            cardInfo.StartTime = cardInfo.StartTime.substr(0, 10);
            cardInfo.EndTime = cardInfo.EndTime.substr(0, 10);
          }
          if (res.data.Data.couponType) {
            // 优惠券
            couponInfo.UseStartTime = couponInfo.UseStartTime.substr(0, 10);
            couponInfo.UseEndTime = couponInfo.UseEndTime.substr(0, 10);
            if (res.data.Data.PayMoney > 0) {
              if (couponInfo.OrderAmountLower > 0) {
                maxCount = that.getMaxCount(res.data.Data.PayMoney, couponInfo.OrderAmountLower, res.data.Data.total.count);
              }
            }
          }
          that.data.maxCount = maxCount;
          that.setData({
            cardInfo: cardInfo,
            couponInfo: couponInfo,
            sumNumber: res.data.Data.total.count,
            money: res.data.Data.PayMoney < 0 ? 0 : res.data.Data.PayMoney
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 确认销券(打折)
  checkCoupon: function () {
    let that = this;
    let money = that.data.money; //应付金额
    let couponInfo = that.data.couponInfo;
    let cardId = that.data.cardid; //折扣卡id
    let couponTypeId = that.data.coupontypeid;
    let remark = that.data.remark;
    if (money == '') {
      money = 0;
    }
    if (!cardId || cardId == '') {
      that.setData({
        cardid: 0
      })
    }
    if (!that.data.checkNumber) {
      that.checkWrite("请输入优惠券张数");
      return;
    }
    if (money % 1 != 0) {
      var checkMoney = that.checkNumbers(money);
      if (!checkMoney) {
        that.checkWrite("请输入正确的金额");
        return;
      }
    }
    if (couponTypeId > 0) {
      if (couponInfo.Money > 0) {
        if (money < couponInfo.OrderAmountLower) {
          that.checkWrite("应付金额不能小于优惠券金额！");
          return
        }
      }
      if (that.data.maxCount < that.data.checkNumber || that.data.sumNumber < that.data.checkNumber) {
        that.checkWrite("本次消费最多可使用" + that.data.maxCount + "张优惠券");
        return
      }
    }
    wx.request({
      url: app.globalData.host + '/api/Coupon/PackageCheck',
      data: {
        createUid: that.data.creatUid,
        uid: that.data.userid,
        checkUid: app.globalData.userInfo.Uid,
        payMoney: parseFloat(money),
        count: that.data.checkNumber,
        cardId: that.data.cardid,
        couponId: that.data.couponid
      },
      method: 'POST',
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data) {
            money = parseFloat(res.data.Data.PayMoney - res.data.Data.CheckMoney).toFixed(2);
            that.data.checkId = res.data.Data.Id;
            that.setData({
              checked: true,
              money: money < 0 ? 0 : money
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 确认结算
  submitCheck: function () {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/SubmitPackageCheck?checkId=' + that.data.checkId + '&payMoney=' + that.data.money,
      success: function (res) {
        if (res.data.Code == 0) {
          wx.showToast({
            title: '结算成功',
          })
          that.setData({
            showDialog: !that.data.showDialog
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 继续核销
  continueCheck: function () {
    wx.scanCode({
      success(res) {
        let url = res.path;
        setTimeout(function () {
          wx.navigateTo({
            url: url,
          })
        }, 1000)
      }
    })
  },

  getCouponInfo: function (couponid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCouponInfo?uid=' + that.data.userid + '&couponId=' + couponid,
      success: function (res) {
        if (res.data.Code == 0) {
          that.setData({
            couponsn: res.data.Data.Coupon.CouponSN,
            creatUid: res.data.Data.Coupon.CreateUid
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 最大可用优惠券张数
  getMaxCount: function (PayMoney, OrderAmountLower, totalCount) {
    let maxCount = parseInt(PayMoney / OrderAmountLower);
    if (totalCount < maxCount) {
      maxCount = totalCount;
    }
    return maxCount;
  },

  // 减
  numJianTap: function () {
    if (this.data.checkNumber > 1) {
      var currentNum = this.data.checkNumber;
      currentNum--;
      this.setData({
        checkNumber: currentNum
      })
    }
  },
  // 加
  numJiaTap: function (e) {
    if (this.data.checkNumber < this.data.sumNumber) {
      var currentNum = this.data.checkNumber;
      currentNum++;
      this.setData({
        checkNumber: currentNum
      })
    }
  },
  bindnumber: function (e) {
    this.setData({
      checkNumber: e.detail.value
    })
  },
  // 应付金额
  bindmoney: function (e) {
    let that = this;
    let money = e.detail.value; //应付金额
    let couponInfo = that.data.couponInfo;
    let count = 1;
    let maxCount = 1;
    let cardId = that.data.cardid; //折扣卡id
    let couponId = that.data.coupontypeid;
    if (money == '') {
      money = 0;
    }
    if (!cardId || cardId == '') {
      cardId = 0;
      count = that.data.checkNumber;
    }
    if (!couponId || couponId == '') {
      couponId = 0;
    }
    if (couponInfo) {
      if (couponId > 0 && couponInfo.OrderAmountLower > 0) {
        maxCount = that.getMaxCount(money, couponInfo.OrderAmountLower, that.data.sumNumber);
      }
    }
    this.data.maxCount = maxCount;
    this.data.cardid = cardId;
    this.setData({
      checkNumber: maxCount,
      money: e.detail.value
    })
  },

  showDialog: function () {
    let cardId = wx.getStorageSync("cardId");
    this.setData({
      showDialog: !this.data.showDialog
    })
    wx.navigateTo({
      url: '/pages/home/index?id=' + cardId,
    })
  },
  // 再发一张
  sendAgain: function () {
    wx.navigateTo({
      url: '/merchant/merchants/provide/index?useuid=' + this.data.userid,
    })
    this.setData({
      showDialog: !this.data.showDialog
    })
  },
  //填写判断
  checkWrite: function (str) {
    wx.showModal({
      title: '提示',
      content: str,
      showCancel: false
    })
  },
  // 小数点验证
  checkNumbers: function (value) {
    var returnVal = true;
    var inputZ = value;
    var ArrMen = inputZ.split("."); //截取字符串
    if (ArrMen.length > 2) {
      returnVal = false;
    }
    return returnVal;
  },
})